package com.iwdnb.gkgz.common.enums;

import java.math.BigDecimal;
import java.util.List;

import com.iwdnb.bmnf.common.enums.CodeMessage;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.utils.BigDecimalUtils;
import com.iwdnb.gkgz.common.utils.StockUtils;

import static com.iwdnb.gkgz.common.utils.StockUtils.getAveragePrice;

public enum TrendTypeEnums implements CodeMessage {
    DOWN("down", "下跌"),
    DOWN_UP("downup", "下跌后上涨"),
    DOWN_UP_DOWN_BIG("dudb", "先跌后涨再跌更多"),
    DOWN_UP_DOWN_SMALL("duds", "先跌后涨再跌回少许"),
    UP("up", "上涨"),
    UP_DOWN("updown", "上涨后下跌"),
    UP_DOWN_UP_BIG("udub", "先涨后跌再涨更多"),
    UP_DOWN_UP_SMALL("udus", "先涨后跌再涨回少许"),
    ;

    private String code;
    private String message;

    TrendTypeEnums(String code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public static TrendTypeEnums getTrendType(List<StockDayData> stockDayDataList, StockDayData data, int period) {
        stockDayDataList = stockDayDataList.subList(stockDayDataList.size() - 20, stockDayDataList.size());
        StockDayData thirtyAgoData = stockDayDataList.get(0);
        StockDayData tenAgoData = stockDayDataList.get(10);
        StockDayData fiveAgoData = stockDayDataList.get(5);
        BigDecimal currentAvg = getAveragePrice(data, period);
        BigDecimal thirtyAgoAvg = getAveragePrice(thirtyAgoData, period);
        BigDecimal tenAgoAvg = getAveragePrice(tenAgoData, period);
        BigDecimal fiveAgoAvg = getAveragePrice(fiveAgoData, period);
        return TrendTypeEnums.getTrendType(thirtyAgoAvg, tenAgoAvg, fiveAgoAvg, currentAvg);
    }

    public static TrendTypeEnums getTrendType(BigDecimal firstPrice, BigDecimal secondPrice, BigDecimal threePrice,
        BigDecimal currentPrice) {
        boolean firstRangeUpFlag = BigDecimalUtils.isGe(secondPrice, firstPrice);
        boolean secondRangeUpFlag = BigDecimalUtils.isGe(threePrice, secondPrice);
        boolean thirdUpFlag = BigDecimalUtils.isGe(currentPrice, threePrice);

        if (firstRangeUpFlag) {
            //波段一上涨,波段二上涨
            if (secondRangeUpFlag) {
                if (thirdUpFlag) {
                    return UP;
                } else {
                    return UP_DOWN;
                }
            } else if (!thirdUpFlag) {
                return UP_DOWN;
            } else if (BigDecimalUtils.isGe(currentPrice, secondPrice)) {
                return UP_DOWN_UP_BIG;

            }
            return UP_DOWN_UP_SMALL;
        } else {
            //波段一下跌,波段二下跌
            if (!secondRangeUpFlag) {
                if (!thirdUpFlag) {
                    return DOWN;
                } else {
                    return DOWN_UP;
                }
            } else if (thirdUpFlag) {
                return DOWN_UP;
            } else if (BigDecimalUtils.isLt(currentPrice, secondPrice)) {
                return DOWN_UP_DOWN_BIG;
            }
            return DOWN_UP_DOWN_SMALL;
        }
    }

    public boolean isUp() {
        return this == DOWN_UP || this == UP || this == UP_DOWN_UP_BIG || this == UP_DOWN_UP_SMALL;
    }

    public boolean isDown() {
        return this == UP_DOWN || this == DOWN || this == DOWN_UP_DOWN_BIG
            || this == DOWN_UP_DOWN_SMALL;
    }
}
